package com.itheima.service;

import com.itheima.Repository.UserRepository;
import com.itheima.domain.User;
import com.itheima.domain.UserVO;
import com.itheima.utils.MyBeanUtils;
import javassist.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public List<User> findAll(){
        return userRepository.findAll();
    }

    /**
     * 组合查询
     * @param userVO  组合查询条件
     * @return 查询结果
     */
    public List<User> listsearch(UserVO userVO) {
        return userRepository.findAll(new Specification<User>(){
            @Override
            public Predicate toPredicate(Root<User> root,
                                         CriteriaQuery<?> query,
                                         CriteriaBuilder criteriaBuilder) {
                //数组
                List<Predicate> predicates=new ArrayList<>();
                if(!"".equals(userVO.getUsername())&&userVO.getUsername()!=null){
                    predicates.add(criteriaBuilder.like(root.get("username"),"%"+userVO.getUsername()+"%"));
                }

//                if(!"".equals(userQueryVO.getCity())&&userQueryVO.getCity()!=null){
//                    predicates.add(criteriaBuilder.like(root.get("city"),"%"+userQueryVO.getCity()+"%"));
//                }
//                if(!"".equals(userQueryVO.getClassify())&&userQueryVO.getClassify()!=null){
//                    predicates.add(criteriaBuilder.like(root.get("classify"),"%"+userQueryVO.getClassify()+"%"));
//                }
                //组合查询，转为数组
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        });
    }

    /**
     * 新增用户
     */
    @Transactional
    public void addUser(User user){
        userRepository.save(user);
    }

    /**
     * 根据 ID 查询用户
     */
    public User getUser(Integer id){
        return userRepository.findOne(id);
    }

    /**
     * 根据 ID 修改用户信息
     */
    @Transactional
    public User updateById(Integer id,User user) throws NotFoundException {
        User user1=userRepository.findOne(id);
        if(user1==null){
            throw new NotFoundException("无此用户");
        }
        //过滤属性为空的属性，只copy有属性值的
        BeanUtils.copyProperties(user,user1, MyBeanUtils.getNullPropertyNames(user));
        //user1.setBirthday(new Date());
        return userRepository.save(user);
    }

    /**
     * 根据 ID 删除用户
     */
    @Transactional
    public void deleteById(Integer id){
        userRepository.delete(id);
    }

    /**
     * 批量删除
    public void delAllUser(List<Integer> delList){
        userRepository.delAllUser(delList);
    }
     */
}
